package controllers;

import java.io.IOException;
import java.rmi.ServerException;
import java.util.TreeMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import models.UserModel;

import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

import entities.User;
import forms.CaptchaForm;
import forms.UserForm;

public class RegisterServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
		HttpSession session = request.getSession();
		TreeMap check = new TreeMap();
		
		//Check permission
		if (request.getUserPrincipal() == null) {
			UserService userService = UserServiceFactory.getUserService();
			String thisURL = request.getRequestURI();
			String url = userService.createLoginURL(thisURL);
			response.sendRedirect(url);
			return;
		}
		
		String username = request.getUserPrincipal().getName();
		UserModel userModel = UserModel.getInstance();
		User user = userModel.getUser(username);
		if (user != null) {
			response.sendRedirect("/profile");
			return;
		} else {
			user = new User();
			user.setUsername(username);
		}
		
		
		request.setAttribute("user", user);
		request.setAttribute("check", check);
		request.setAttribute("form", "add");
		
		try {
			request.getRequestDispatcher("/pages/register.jsp").forward(request, response);
		} catch (ServletException e) {
			e.printStackTrace();
		}
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
		
		//Check permission
		if (request.getUserPrincipal() == null) {
			UserService userService = UserServiceFactory.getUserService();
			String thisURL = request.getRequestURI();
			String url = userService.createLoginURL(thisURL);
			response.sendRedirect(url);
			return;
		}
		
		String username = request.getUserPrincipal().getName();
		UserModel uData = UserModel.getInstance();
		User user = uData.getUser(username);
		if (user != null) {
			response.sendRedirect("/profile");
			return;
		} else {
			user = new User();
			user.setUsername(username);
		}
		
		TreeMap forms = (TreeMap) UserForm.checkValid(request, user);
		user = (User) forms.get("user");
		TreeMap check = (TreeMap) forms.get("checkValid");
		check = CaptchaForm.checkValid(request, check);
		
		if (check.size() == 0) {
			user.setRole(0);
			user.setStatus(2);
			uData.insert(user);
			
			HttpSession session = request.getSession();
			session.setAttribute("user_logged_in", user);
			session.setAttribute("success", "<b>You has been register success.");
			response.sendRedirect("/profile");
			return;
		}
		
		request.setAttribute("user", user);
		request.setAttribute("check", check);
		request.setAttribute("form", "add");
		
		try {
			request.getRequestDispatcher("/pages/register.jsp").forward(request, response);
		} catch (ServletException e) {
			e.printStackTrace();
		}
	}
}
